<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Update a topic
*/

/**
 * Update a tag
 * @author John Cox
 * @author Jo dalle Nogare
 * @param $args['id'] integer
 * @param $args['tag'] string
 * @param $args['timestamp'] integer
 * @param $args['lastvisit'] integer
 * @param $args['status'] integer
 * @param $args['uid'] integer
 * @param $args['count'] integer
 * @param $args['options'] string
 * @returns int
 * @return true on success, NULL on failure
 * @todo support an 'options' array that merges into an existing 'toptions' array.
 */

function conversations_userapi_updatetag($args)
{
    // Get arguments from argument array
    extract($args);
    
    if (!isset($id)){
        $id = $tagid;
    }

    if (!isset($id)) $invalid[] = "id";

    // params in arg
    $params = array(
        "id"     => "xar_id",
        "tag"       => "xar_tag",
        "timestamp" => "xar_timestamp",
        "timestamp" => "xar_lastvisit",
        "status"    => "xar_status",
        "uid"       => "xar_uid",
        "count"     => "xar_count",
        "options"   => "xar_options"
    );

    foreach($params as $vvar => $dummy) {
        if (isset($args[$vvar])) {
            $set = true;
            break;
        }
    }

    if (!isset($set)) {
        $invalid[] = xarML('At least one parameter has to be set: #(1)', join(',', array_keys($fields)));
    }
    // Argument check - make sure that at least on paramter is present
    // if not then set an appropriate error message and return
    if (isset($invalid)) {
        $msg = xarML('Invalid parameter count');
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }
    
    // Update the count                                      
    if (isset($count)){
                                           
        $counttot = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                               'id' => $id));
        //return var_export($counttot);
        foreach ($counttot as $goofy){
            $oct = $goofy['count'];
            if (empty($oct)){
                $oct = 1;
            }
        }

        $newcount = $count + $oct;
        //return var_export($newcount);
    }
    
    if (isset($tag)){
        $tag = str_replace('/','_',$tag);
        $tag = str_replace('\\','_',$tag);
    }

    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['ctags'];

    if ((empty($timestamp)) && (empty($options))) {
        $timestamp = time();
    }

    $update = array();
    $bindvars = array();

    // Update item
    $query = "UPDATE $table SET ";
    if (isset($id)){
        $update[] = "xar_id = ? ";
        $bindvars[] = $id;
    }
    if (isset($tag)){
        $update[] = "xar_tag = ? ";
        $bindvars[] = $tag;
    }
    if (isset($timestamp)){
        $update[] = "xar_timestamp = ? ";
        $bindvars[] = $timestamp;
    }
    if (isset($lastvisit)){
        $update[] = "xar_lastvisit = ? ";
        $bindvars[] = $lastvisit;
    }
    if (isset($status)){
        $update[] = "xar_status = ? ";
        $bindvars[] = $status;
    }
    if (isset($uid)){
        $update[] = "xar_uid = ? ";
        $bindvars[] = $uid;
    }
    if (isset($newcount)){
        $update[] = "xar_count = ? ";
        $bindvars[] = $newcount;
    }
    if (isset($options)){
        $update[] = "xar_options = ? ";
        $bindvars[] = $options;
    }
    $query .= join(",",$update);
    $query .= "WHERE xar_id = ? ";
    $bindvars[] = $id;
    $result =& $dbconn->Execute($query, $bindvars);
    if (!$result) return;
    // Let any hooks know that we have created a new topic
    $args['module'] = 'conversations';
    $args['itemtype'] = 1; //tag
    $args['itemid'] = $id;
    xarModCallHooks('item', 'update', $id, $args);
    // Return the id of the newly created link to the calling process
    return true;
}
?>